package com.edu.buservice.web.controller.sa;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-02-09 15:13
 **/

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.sso.config.SaSsoServerConfig;
import cn.dev33.satoken.sso.processor.SaSsoServerProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Sa-Token-SSO Server端 Controller
 */
@RestController
public class SsoServerController {

	/**
	 * SSO-Server端：处理所有SSO相关请求 (下面的章节我们会详细列出开放的接口)
	 */
	@RequestMapping("/sso/*")
	public Object ssoRequest() {
		return SaSsoServerProcessor.instance.dister();
	}

	/**
	 * 配置SSO相关参数
	 */
	@Autowired
	private void configSso(SaSsoServerConfig ssoServer) {
		// 配置：未登录时返回的View
		ssoServer.notLoginView = () -> {
			String redirect = SaHolder.getRequest().getParam("redirect");
			String loginUrl = "/login?redirect="+redirect;
			String msg = "当前会话在SSO-Server端尚未登录，请先访问"
					+ "<a href='%s' target='_blank'> doLogin登录 </a>"
					+ "进行登录之后，刷新页面开始授权";
			msg = String.format(msg, loginUrl);

			return msg;
		};

	}

}
